package caipu;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import com.google.common.collect.Lists;

import uitl.penuitl;

public class AddressBook implements IZ {
	static List<Address> list = Lists.newArrayList();

	@Override
	public void Add(int id, String name, double price) {
		Address address = new Address(id, name, price);
		list.add(address);
		try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IO\\TXL.txt"))) {
			oos.writeObject(list);
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("添加成功!");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void Delete(int id) {
		List<Address> list = null;
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IO\\TXL.txt"));) {
			list = (List<Address>) ois.readObject();
			System.out.println(list);
			for (Address s : list) {
				if (s.getId() == id) {
					list.remove(s);
					System.out.println("删除成功:");
					break;
				}
			}
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IO\\TXL.txt"));
			oos.writeObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public void Modify(int id, String name, double price) {
		List<Address> list = null;
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IO\\TXL.txt"));) {
			list = (List<Address>) ois.readObject();
			for (Address s : list) {
				if (s.getId() == id) {
					s.setId(id);
					s.setName(name);
					s.setPrice(price);
					System.out.println("修改成功:");
					break;
				}
			}
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IO\\TXL.txt"));
			oos.writeObject(list);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public void Qyery(int ip) {
		List<Address> list = null;
		Integer name = null;
		String name1 = null;
		Double name2 = null;
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IO\\TXL.txt"))) {
			list = (List<Address>) ois.readObject();
			if (list == null) {
				System.out.println("菜谱不存在...");
			}
			if (ip == 1) {
				name = penuitl.penInt("          请输入菜谱编号:");
			} else if (ip == 2) {
				name1 = penuitl.penString("          请输入菜谱名称:");
			} else if (ip == 3) {
				name2 = penuitl.penDouble("          请输入菜谱价格:");
			}
			for (Address s : list) {
				if (ip == 1) {
					if (s.getId() == name) {
						System.out.println(s);
						break;
					} else {
						System.out.println("菜谱不存在...");
					}
				} else if (ip == 2) {
					if (s.getName().equals(name1)) {
						System.out.println(s);
						break;
					} else {
						System.out.println("菜谱不存在...");
					}
				} else if (ip == 3) {
					if (s.getPrice() == name2) {
						System.out.println(s);
						break;
					} else {
						System.out.println("菜谱不存在...");
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public void YL() {
		List<Address> list = null;
		try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IO\\TXL.txt"));) {
			list = (List<Address>) ois.readObject();
			for (Address s : list) {
				System.out.println(s);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}